package com.rao.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rao.gmall.model.product.BaseCategoryView;
import com.rao.gmall.model.product.SkuImage;
import com.rao.gmall.model.product.SkuInfo;
import com.rao.gmall.model.product.SpuSaleAttr;
import com.rao.gmall.product.bean.SkuAllSaleValue;
import com.rao.gmall.product.mapper.ProductApiServiceMapper;
import com.rao.gmall.product.mapper.SkuImageMapper;
import com.rao.gmall.product.mapper.SkuInfoMapper;
import com.rao.gmall.product.mapper.SpuSaleAttrMapper;
import com.rao.gmall.product.service.ProductApiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nzmxfan
 * @create 2021-08-06-19:14
 */
@Service
public class ProductApiServiceImpl extends ServiceImpl<ProductApiServiceMapper, BaseCategoryView> implements ProductApiService {

    @Resource
    ProductApiServiceMapper productApiServiceMapper;

    @Resource
    SkuInfoMapper skuInfoMapper;

    @Resource
    SkuImageMapper skuImageMapper;

    @Resource
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        // 获取基本信息
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);

        // 获取图片信息
        QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId);
        List<SkuImage> skuImages = skuImageMapper.selectList(queryWrapper);

        // 将图片信息写入 skuInfo 中
        skuInfo.setSkuImageList(skuImages);

        return skuInfo;
    }

    @Override
    public BaseCategoryView getBaseCategoryView(Long category3Id) {

        return productApiServiceMapper.getBaseCategoryView(category3Id);
    }

    @Override
    public BigDecimal getSkuPrice(Long skuId) {

        return skuInfoMapper.getSkuPrice(skuId);
    }

    /**
     *  根据 skuId 获取该商品的 spu 的所有销售属性和属性值
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {

        return spuSaleAttrMapper.getSpuSaleAttrWithAllValueAndSkuCheck(skuId,spuId);
    }

    @Override
    public Map getSkuValueIdsMap(Long spuId) {

        Map<String, String> map = new HashMap<>();
        List<SkuAllSaleValue> skuValueIdsList = spuSaleAttrMapper.getSkuValueIdsMap(spuId);

        for (SkuAllSaleValue skuAllSaleValue : skuValueIdsList) {

            Long skuId = skuAllSaleValue.getSkuId();
            String valueIds = skuAllSaleValue.getValueIds();
            map.put(valueIds,skuId.toString());
        }
        return map;
    }
}
